package com.fmy.response.call;

import com.fmy.response.specs.IEnum;
import com.fmy.response.specs.Response;
import com.fmy.response.specs.ResponseEnum;

import java.lang.reflect.Array;

/**
 * @version 1.0
 * @Author MR.fmy
 * @Description Response 返回调用类
 * @Date 2019/5/15 0015 09:05
 **/
public class ResponseCall {


    public static Response success() {
        return success(null, "OK");
    }

    public static Response success(Object data) {
        return success(data, "OK");
    }

    public static Response success(Object data, String msg) {
        msg = msg != null ? msg : "OK";
        data = data != null ? data : "";
        return new Response(ResponseEnum.OK.getCode(), msg, data);
    }

    public static Response page(Object data, Object count) {
        return page(ResponseEnum.OK, "OK", data, count);
    }

    public static Response page(String msg, Object data, Object count) {
        return page(ResponseEnum.OK, msg, data, count);
    }

    public static Response pageZero(Object data, Object count) {
        return page(ResponseEnum.OK_ZERO, "OK", data, count);
    }

    public static Response pageZero(String msg, Object data, Object count) {
        return page(ResponseEnum.OK_ZERO, msg, data, count);
    }

    public static Response error(String errMsg) {
        errMsg = errMsg != null ? errMsg : "business_fail";
        return new Response(ResponseEnum.BUSINESS_FAIL.getCode(), errMsg);
    }

    public static Response error(IEnum iEnum, String errMsg) {
        errMsg = errMsg != null ? errMsg : "error";
        if (iEnum == null) {
            return new Response(ResponseEnum.SERVER_ERROR.getCode(), errMsg);
        }
        return new Response(iEnum.getCode(), errMsg);
    }

    private static Response page(IEnum iEnum, String msg, Object data, Object count) {
        msg = msg != null ? msg : "OK";
        data = data != null ? data : new Array[0];
        count = count != null ? count : 0;
        if (count instanceof Long || count instanceof Integer) {
            return new Response(iEnum.getCode(), msg, data, Integer.valueOf(count.toString()));
        } else {
            throw new ClassCastException("Count must be Number type");
        }

    }
}
